package com.lams.action.archive;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.lams.action.SubAction;
import com.lams.service.archive.IStudentFacade;
import com.lams.util.ResultEntity;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class StudentAction extends SubAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7457883008416067997L;
	private IStudentFacade stu;
	private Map params = new HashMap();
	public IStudentFacade getStu() {
		return stu;
	}

	public void setStu(IStudentFacade stu) {
		this.stu = stu;
	}
	
	private String SCHOOL_ID;
	private String CLASS_ID;
	private String STUDENT_ID;
	private String STUDENT_NO;
	private String STUDENT_NAME;
	private String GENDER;
	private String BIRTH_DATE;
	private String GUARDIAN;
	private String GUARDIAN_PHONE;
	private String ADDRESS;
	private String REMARK;
	private File PHOTO;
	private String PHOTOContentType;
	
//	
	public String query(){
		//Map params = ActionContext.getContext().getParameters();
		//params.put("", value)
		ResultEntity result = stu.queryStudent(params, this.getStart(), this.getLimit());
		this.setResult(ResultEntity.decode(result));
		return SUCCESS;
	}
	public String getSTUDENT_NAME() {
		return STUDENT_NAME;
	}

	public void setSTUDENT_NAME(String sTUDENT_NAME) {
		STUDENT_NAME = sTUDENT_NAME;
		params.put("STUDENT_NAME", STUDENT_NAME);
	}

	@SuppressWarnings({ "rawtypes", "unused", "deprecation" })
	public String addOrUpdate(){
		HttpServletRequest request = ServletActionContext.getRequest();
		//上传文件名  取学号_毫秒数
		String fileName = this.getSTUDENT_NO()+".jpg";
		//需上传文件不为空且为图片格式
		List list = new ArrayList();
		if(this.getPHOTO()!=null&&"image/jpeg".equals(this.getPHOTOContentType())){
			String filePath = request.getRealPath("/")+"avatr/";
			stu.uploadPhoto(PHOTO,filePath,fileName);
			Map map = new HashMap();
			map.put("PHOTOFileName", fileName);
			list.add(map);
			params.put("PHOTO", fileName);
		}else{
			params.put("PHOTO", "");
		}
		ResultEntity result = stu.addOrUpdateStudent(params, this.getSTUDENT_ID()==null||"".equals(this.getSTUDENT_ID())||"-1".equals(this.getSTUDENT_ID())?"add":"update");
		result.setData(list);
		this.setResult(ResultEntity.decode(result));
		return SUCCESS;
	}
	
	public File getPHOTO() {
		return PHOTO;
	}

	public void setPHOTO(File pHOTO) {
		PHOTO = pHOTO;
	}

	public String getPHOTOContentType() {
		return PHOTOContentType;
	}

	public void setPHOTOContentType(String pHOTOContentType) {
		PHOTOContentType = pHOTOContentType;
	}

	public String getSCHOOL_ID() {
		return SCHOOL_ID;
	}

	public String getSTUDENT_NO() {
		return STUDENT_NO;
	}

	public void setSTUDENT_NO(String sTUDENT_NO) {
		STUDENT_NO = sTUDENT_NO;
		params.put("STUDENT_NO", STUDENT_NO);
	}

	public String getGENDER() {
		return GENDER;
	}

	public void setGENDER(String gENDER) {
		GENDER = gENDER;
		params.put("GENDER", "男".equals(GENDER)?"1":"女".equals(GENDER)?"2":GENDER);
	}

	public String getBIRTH_DATE() {
		return BIRTH_DATE;
	}

	public void setBIRTH_DATE(String bIRTH_DATE) {
		BIRTH_DATE = bIRTH_DATE;
		params.put("BIRTH_DATE", BIRTH_DATE);
	}

	public String getGUARDIAN() {
		return GUARDIAN;
	}

	public void setGUARDIAN(String gUARDIAN) {
		GUARDIAN = gUARDIAN;
		params.put("GUARDIAN", GUARDIAN);
	}

	public String getGUARDIAN_PHONE() {
		return GUARDIAN_PHONE;
	}

	public void setGUARDIAN_PHONE(String gUARDIAN_PHONE) {
		GUARDIAN_PHONE = gUARDIAN_PHONE;
		params.put("GUARDIAN_PHONE", GUARDIAN_PHONE);
	}

	public String getADDRESS() {
		return ADDRESS;
	}

	public void setADDRESS(String aDDRESS) {
		ADDRESS = aDDRESS;
		params.put("ADDRESS", ADDRESS);
	}

	public String getREMARK() {
		return REMARK;
	}

	public void setREMARK(String rEMARK) {
		REMARK = rEMARK;
		params.put("REMARK", REMARK);
	}

	public void setSCHOOL_ID(String sCHOOL_ID) {
		SCHOOL_ID = "".equals(sCHOOL_ID)?"-1":sCHOOL_ID;
		params.put("SCHOOL_ID", SCHOOL_ID);
	}

	public String getCLASS_ID() {
		return CLASS_ID;
	}

	public void setCLASS_ID(String cLASS_ID) {
		CLASS_ID = "".equals(cLASS_ID)?"-1":cLASS_ID;
		params.put("CLASS_ID", CLASS_ID);
	}

	public String getSTUDENT_ID() {
		return STUDENT_ID;
	}

	public void setSTUDENT_ID(String sTUDENT_ID) {
		STUDENT_ID = "".equals(sTUDENT_ID)?"-1":sTUDENT_ID;
		params.put("STUDENT_ID", STUDENT_ID);
	}

	
}
